package com.wonders.frame.service;

import com.wonders.frame.common.Enum.AuthDicTypeEnum;
import com.wonders.frame.common.Enum.CommonCodeEnum;
import com.wonders.frame.common.Enum.RemovedCodeEnum;
import com.wonders.frame.entity.AuthDic;
import com.wonders.frame.entity.AuthDicType;
import com.wonders.frame.entity.AuthOrga;
import com.wonders.frame.entity.Tree.SelectTreeNodeData;
import com.wonders.frame.entity.form.AuthDicTypeForm;
import com.wonders.frame.repository.AuthDicRepository;
import com.wonders.frame.repository.AuthDicTypeRepository;
import com.wonders.frame.repository.core.CoreRepository;
import com.wonders.frame.service.Core.CoreCurdService;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AuthDicTypeService extends CoreCurdService<AuthDicType> {
    Logger log = LoggerFactory.getLogger(AuthDicTypeService.class);

    @Autowired
    private AuthDicTypeRepository authDicTypeRepository;

    @Autowired
    private AuthDicRepository authDicRepository;

    @Override
    protected CoreRepository<AuthDicType> getRepository() {
        return authDicTypeRepository;
    }

    public Boolean checkUnique(String id,String code){
        List<AuthDicType> list = authDicTypeRepository.checkUnique(code);
        if(Strings.isNotEmpty(id)){
            //ID非空，为修改
            AuthDicType authDicTypeOld = authDicTypeRepository.findById(id).get();
            if(code.equals(authDicTypeOld.getCode())){
                //没修改code直接通过验证
                return true;
            }
        }
        //改过code,或者新增 需要验证
        if(list!=null&&list.size()>0){
            return false;
        }else{
            return true;
        }
    }

    public AuthDicType getOneAuthDicTypeNode(String id){
        if(Strings.isNotEmpty(id)){

            AuthDicType authDicType = authDicTypeRepository.getOneAuthDicTypeById(id);
            return authDicType;
        }
        return null;
    }

    //获取全部字典-树形结构
    public List<SelectTreeNodeData> getAuthDicTypeTreeNode(String type){

        List<SelectTreeNodeData> nodes = new ArrayList<SelectTreeNodeData>();

        List<AuthDicType> authDicTypes = authDicTypeRepository.getAllAuthDicType();

        if(type.equals(AuthDicTypeEnum.PT.getType())){
            for (AuthDicType authDicType : authDicTypes){
                if(authDicType.getParentId() == null && authDicType.getDicType().equals(AuthDicTypeEnum.PT.getType())){
                    nodes.add(getAuthDicTypeNode(authDicType));
                }
            }
        }else if(type.equals(AuthDicTypeEnum.SYSTEM.getType())){
            for (AuthDicType authDicType : authDicTypes){
                if(authDicType.getParentId() == null && authDicType.getDicType().equals(AuthDicTypeEnum.SYSTEM.getType())){
                    nodes.add(getAuthDicTypeNode(authDicType));
                }
            }
        }
        return nodes;
    }

    public AuthDicType saveAuthDicType(AuthDicTypeForm authDicTypeForm){
        AuthDicType authDicType = new AuthDicType();
        BeanUtils.copyProperties(authDicTypeForm,authDicType);
        if(StringUtils.isEmpty(authDicType.getId())){
            //新增
            log.debug("new AuthDicType");
            authDicType.setRemoved(RemovedCodeEnum.AVAILABLE.getCode());
            authDicType.setCrtDate(new Date());
        }else {
            //修改,验证除自身重复
            log.debug("modify authDicType");
            String codeNew = authDicType.getCode();
            String codeOld = authDicTypeRepository.findById(authDicType.getId()).get().getCode();
            authDicType.setUpdDate(new Date());
            //查所有子节点 不设置的话 会把子节点的parentId赋空
            List<AuthDicType> list = authDicTypeRepository.findByParentIdAndRemoved(authDicType.getId(),RemovedCodeEnum.AVAILABLE.getCode());
            authDicType.setAuthDicTypeChildren(list);

            //修改字典类型动态修改关联的dic
            List<AuthDic> dicList = authDicRepository.getByDicType(codeOld);
            for(AuthDic dic:dicList){
                dic.setDicType(codeNew);
                authDicRepository.save(dic);
            }
        }
        return authDicTypeRepository.save(authDicType);
    }


    //逻辑删除
    public CommonCodeEnum deleteAuthDicTypeById(String dicTypeId){
        //先根据id获得此值
        try {
            AuthDicType authDicType = authDicTypeRepository.findById(dicTypeId).get();
            authDicType.setRemoved(1);
            authDicTypeRepository.save(authDicType);
            List<AuthDic> authDicList = authDicRepository.getByDicType(authDicType.getCode());
            for(AuthDic authDic:authDicList){
                authDic.setRemoved(1);
                authDicRepository.save(authDic);
            }
            return CommonCodeEnum.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return CommonCodeEnum.ERROR;
        }
    }



    /**
     * 递归调用，初始化字典树对象
     */
    public SelectTreeNodeData getAuthDicTypeNode(AuthDicType authDicType){
        SelectTreeNodeData nodeData = new SelectTreeNodeData();
        nodeData.setKey(authDicType.getId());
        nodeData.setValue(authDicType.getId());
        nodeData.setTitle(authDicType.getName());
        nodeData.setIcon(authDicType.getDisplaytype());
        //有子节点
        if(authDicType.getAuthDicTypeChildren() != null){
            List<SelectTreeNodeData> nodes = new ArrayList<SelectTreeNodeData>();
            for(AuthDicType o : authDicType.getAuthDicTypeChildren()){
                //且判断removed是否被删除
                if(o.getRemoved()==0){
                    nodes.add(getAuthDicTypeNode(o));
                }
            }
            nodeData.setChildren(nodes);
        }
        return nodeData;
    }

    //模糊查询字典类型节点
    public List<SelectTreeNodeData> findNodeByDicName(String dicName){
        List<AuthDicType> list = authDicTypeRepository.findByNameLikeAndRemoved("%"+dicName+"%",0);
        List<SelectTreeNodeData> nodes = new ArrayList<SelectTreeNodeData>();
        for(AuthDicType authDicType:list){
                SelectTreeNodeData nodeData = new SelectTreeNodeData();
                nodeData.setKey(authDicType.getId());
                nodeData.setValue(authDicType.getId());
                nodeData.setTitle(authDicType.getName());
                nodeData.setChildren(new ArrayList<>());
                nodes.add(nodeData);
            }
        return nodes;
    }


}
